<?php

// 参考：http://www.php.net/manual/zh/pdo.connections.php
// https://segmentfault.com/a/1190000004106175

include_once 'db_config.php';
// array(PDO::ATTR_PERSISTENT => true) 开启持久化连接
$dbh = new PDO('mysql:host=localhost;dbname=' . $db['db_name'], $db['user'], $db['pwd'], array(PDO::ATTR_PERSISTENT => true));

// 获取初始年月到当前年月日 http://www.itnose.net/detail/6066165.html
// 获取指定日期的净增用户总量
$next_day = date("Y-m-d", strtotime('-7 days'));
$end_day = date('Y-m-d', strtotime('-1 days'));
$sql = 'SELECT * FROM `user` where ref_date>="' . $next_day . '" and ref_date<="' . $end_day . '"';
echo '<table><tbody><thread><tr><td>日期</td><td>新增用户数</td><td>取消用户数</td><td>净增用户数</td><td>总用户数</td></tr></thead>';
foreach ($dbh->query($sql) as $row) {
    // 把日期和用户数单独组成列 | 变量追加到数组中
    echo '<tr>';
    echo '<td>' . $row['ref_date'] . '</td>';
    echo '<td>' . $row['new_user'] . '</td>';
    echo '<td>' . $row['cancel_user'] . '</td>';
    echo '<td>' . $row['net_user'] . '</td>';
    echo '<td>' . $row['cumulate_user'] . '</td>';
    echo '</tr>';
}
echo '</tbody></table>';
$dbh = null;
?>